from app import db

class QuestionTag(db.Model):
    """问题-标签关联表模型"""
    __tablename__ = 'question_tags'
    
    id = db.Column(db.Integer, primary_key=True)
    question_id = db.Column(db.Integer, db.ForeignKey('questions.id', ondelete='CASCADE'), nullable=False, index=True)
    tag_id = db.Column(db.Integer, db.ForeignKey('tags.id', ondelete='CASCADE'), nullable=False, index=True)
    
    # 创建联合唯一索引，确保同一个问题不会重复添加同一个标签
    __table_args__ = (
        db.UniqueConstraint('question_id', 'tag_id', name='uix_question_tag'),
    )
    
    def __repr__(self):
        return f'<QuestionTag {self.question_id}-{self.tag_id}>' 